package org.example.Server.Impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.example.Mapper.StudentMapper;
import org.example.Pojo.Student;
import org.example.Server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class StudentServerImpl implements StudentServer {

    @Autowired
    private StudentMapper studentMapper;


    /*
    * 获取学生列表
    * */
    @Override
    public List<Student> getStudents(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Student> students = studentMapper.getStudents(name, degree, clazzId);
        return students;
    }


    /*
    * 添加学生
    * */
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.addStudent(student);
    }

    /*
    * 查询学生通过Id
    * */
    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.getStudentById(id);
    }

    /*
     * 更新学生信息
     * */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /*
    * 删除学生
    * */
    @Override
    public void deleteStudent(List<Integer> ids) {
        studentMapper.deleteStudent(ids);
    }


    /*
    * 违纪处理
    * */
    @Transactional
    @Override
    public void violationStudent(Integer id, Integer score) {
        //获取对应学员数据
        Student student = studentMapper.getStudentById(id);
        //处理学员的数据
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) (student.getViolationCount()+1));
        student.setViolationScore((short) (student.getViolationScore()+score));
        //更新数据
        studentMapper.updateStudent(student);
    }
}
